# Descriptive

parties <- c("Republican", "Democrat")
p_party <- c(.5, .5)

# Gender: 

genders <- c("a man", "a woman")
p_gender <- c(.5, .5)

# Ethnicity: 

ethnicities <- c("White","Black", "Asian", "Hispanic")
p_ethnicity <- c(.63, .13, .06, .18) 
# https://www.census.gov/quickfacts/fact/table/US/RHI125219#qf-headnote-b

# Class: 

class <- c("lower class","working class", "middle class", "upper class")
p_class <- c(1/4, 1/4, 1/4, 1/4)

# Sexuality

sexuality <- c("heterosexual", "homosexual")
p_sexuality <- c(.9, .1) 

characteristics <- c("gender", "ethnicity", "sexuality", "class")

surrogation <-
  c(
    "the congressman <strong>for your congressional district</strong>.",
    "a congressman, <strong>but not for your congressional district</strong>."
  )

justification <-
  c(
    "regularly emphasises the benefits of their political positions for <strong>people like you</strong>.",
    "regularly emphasises the benefits of their political positions for <strong>society as a whole</strong>.",
    "regularly explains how their policies matter to <strong>people like you</strong>.",
    "regularly explains how their policies matter to <strong>society as a whole</strong>.",
    "regularly justifies their decisions on the basis of what is good for <strong>particular groups</strong> in society.",
    "regularly justifies their decisions on the basis of what is good for <strong>the whole of the US</strong>.",
    "regularly explains how their decisions help to promote <strong>the interests of people like you</strong>.",
    "regularly explains how their decisions help to promote <strong>the national interest</strong>."
  )


substantive <-
  c(
    "abolishing the death penalty for persons convicted of murder.",
    "introducing more stringent measures to reduce immigration.",
    "increasing the federal tax rate on income over $510,000 to 40%.",
    "increasing federal spending on welfare programs.",
    "introducing a federal carbon tax.",
    "creating a federal school voucher program."
  )

responsiveness <-
  c(
    '"I always <strong>listen to voters\' views</strong>, because at the end of the day they get to choose whether to reelect me or not."',
    '"Elections are important because they force me as a politician to <strong>listen and respond to the views of voters</strong>."',
    '"In a democracy, the voter has the last word. That is why I always <strong>listen and respond to what voters want</strong>."',
    '"I can only change society if I am re-elected. So, I always make sure that I <strong>address voters\' most important concerns</strong>."',
    '"I did not go into politics to win elections, but to <strong>make this country a better place</strong>."',
    '"I don\'t care about making popular decisions to win elections, but about <strong>standing up for my principles and values</strong>."',
    '"If I <strong>lose an election</strong> doing what I think is right, then <strong>so be it</strong>."',
    '"I went into politics to <strong>implement good ideas, not popular ones</strong> that win elections."'
  )

personalization <-
  c(
    "<strong>frequently</strong> speaks out and votes <strong>against their party leadership</strong>.",
    "<strong>rarely</strong> speaks out and votes <strong>against their party leadership</strong>.",
    "<strong>usually votes according to party instructions</strong>, even when that conflicts with the wishes of their constituents.",
    "usually votes according to the wishes of their constituents, <strong>even when that conflicts with party instructions</strong>.",
    "<strong>usually votes according to party instructions</strong>, even when that conflicts with their own beliefs.",
    "usually votes according to their own beliefs, <strong>even when that conflicts with party instructions</strong>."
  )

get_mp_pair_vingette <- function(row_order){
  
  # Sample party
  party_tmp <- sample(parties, 2, replace = T, prob = p_party)
  
  # Sample descriptive characteristics
  gender_tmp <- sample(genders, 2, replace = T, prob = p_gender)
  ethnicity_tmp <- sample(ethnicities, 2, replace = T, prob = p_ethnicity)
  sexuality_tmp <- sample(sexuality, 2, replace = T, prob = p_sexuality)
  class_tmp <- sample(class, 2, replace = T, prob = p_class)
  
  # Sample characteristics to include in choice task
  desc_chars_in_task <- data.frame(gender_tmp, data.frame(ethnicity_tmp, sexuality_tmp, class_tmp)[,sample(1:3,1, replace = FALSE)])
  
  descriptive_1_tmp <- desc_chars_in_task[,1]
  descriptive_2_tmp <- desc_chars_in_task[,2]
  
  
  # Sample personalization
  personalization_tmp <- sample(personalization, 2, replace = T)
  
  # Sample surrogation
  surrogation_tmp <- sample(surrogation, 2, replace = T)
  
  # Sample substantive issue
  substantive_tmp <- sample(substantive, 1, replace = T)
  
  # Sample substantive issue position
  substantive_tmp <- paste0(sample(c("<strong>supports</strong>", "<strong>opposes</strong>"), 2, replace = T), " ", substantive_tmp)
  
  # Sample responsiveness
  responsiveness_tmp <- sample(responsiveness, 2, replace = T)
  
  # Sample justification
  justification_tmp <- sample(justification, 2, replace = T)
  
  he_she_tmp <- ifelse(gender_tmp == "a man", "He", "She")
  his_her_tmp <- ifelse(gender_tmp == "a man", "His", "Her")
  himself_herself_tmp <- ifelse(gender_tmp == "a man", "himself", "herself")
  
  
  # Construct sentences
  sentence_one <- paste0("Politician ",1:2,"...")
  
  descriptive_sentence <- paste0("...is <strong>",descriptive_1_tmp, "</strong> and <strong>", descriptive_2_tmp, "</strong>.")
  
  descriptive_sentence <- gsub("<strong>a ", "a <strong>", descriptive_sentence)
  
  surrogation_tmp[1] <- ifelse(grepl("the congressman", surrogation_tmp[1]), 
                               gsub("the congressman",paste0("the <strong>", party_tmp[1], "</strong> congressman"), surrogation_tmp[1]),
                               gsub("a congressman",paste0("a <strong>", party_tmp[1], "</strong> congressman"), surrogation_tmp[1]))
  
  surrogation_tmp[2] <- ifelse(grepl("the congressman", surrogation_tmp[2]), 
                               gsub("the congressman",paste0("the <strong>", party_tmp[2], "</strong> congressman"), surrogation_tmp[2]),
                               gsub("a congressman",paste0("a <strong>", party_tmp[2], "</strong> congressman"), surrogation_tmp[2]))
  
  surrogation_tmp
  
  surrogation_tmp[1] <- ifelse(gender_tmp[1] == "a woman", gsub("congressman","congresswoman", surrogation_tmp[1]), surrogation_tmp[1])
  surrogation_tmp[2] <- ifelse(gender_tmp[2] == "a woman", gsub("congressman","congresswoman", surrogation_tmp[2]), surrogation_tmp[2])
  
  surrogation_sentence <- paste0("...is ", surrogation_tmp)
  
  justification_sentence <- paste0("...", justification_tmp)
  
  substantive_sentence <- paste0("...", substantive_tmp)
  
  responsiveness_sentence <- paste0("...said in a recent interview, ", responsiveness_tmp)
  
  personalization_sentence <- paste0("...", personalization_tmp)
  
  mp_one <- c(sentence_one[1], descriptive_sentence[1], surrogation_sentence[1], justification_sentence[1], responsiveness_sentence[1], personalization_sentence[1], substantive_sentence[1])
  
  mp_two <- c(sentence_one[2], descriptive_sentence[2], surrogation_sentence[2], justification_sentence[2], responsiveness_sentence[2], personalization_sentence[2], substantive_sentence[2])
  
  mp_one <- mp_one[c(1, row_order)]
  mp_two <- mp_two[c(1, row_order)]
  
  out <- c(mp_one, mp_two)
  
  names(out) <- c("mp_1_sentence_1",
                  "mp_1_sentence_2",
                  "mp_1_sentence_3",
                  "mp_1_sentence_4",
                  "mp_1_sentence_5",
                  "mp_1_sentence_6",
                  "mp_1_sentence_7",
                  "mp_2_sentence_1",
                  "mp_2_sentence_2",
                  "mp_2_sentence_3",
                  "mp_2_sentence_4",
                  "mp_2_sentence_5",
                  "mp_2_sentence_6",
                  "mp_2_sentence_7")
  
  as.list(out)
  
}


#* Echo back the input
#* #* @serializer json
#* @get /test
function() {
  
  rows <- sample(2:7)
  
  out1 <- get_mp_pair_vingette(row_order = rows)
  out2 <- get_mp_pair_vingette(row_order = rows)
  out3 <- get_mp_pair_vingette(row_order = rows)
  out4 <- get_mp_pair_vingette(row_order = rows)
  out5 <- get_mp_pair_vingette(row_order = rows)
  
  names(out1) <- paste0(names(out1), "_1")
  names(out2) <- paste0(names(out2), "_2")
  names(out3) <- paste0(names(out3), "_3")
  names(out4) <- paste0(names(out4), "_4")
  names(out5) <- paste0(names(out5), "_5")
  
  out <- c(out1, out2, out3, out4, out5)
  
  out 
  
}
